import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/api/service/activity_service.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/activity_detail_entity.dart';
import 'package:hm_show_flutter/page/goods/detail_swiper.dart';
import 'package:hm_show_flutter/widgets/empty_view.dart';
import 'package:hm_show_flutter/widgets/loading_dialog.dart';

///活动详情
class ActivityDetailPage extends StatefulWidget {
  final int id;

  ActivityDetailPage(this.id);

  @override
  _ActivityDetailPageState createState() => _ActivityDetailPageState();
}

class _ActivityDetailPageState extends State<ActivityDetailPage> {
  ActivityService _service = new ActivityService();
  ActivityDetailDataActivity _data;
  var _detailFuture;

  @override
  void initState() {
    super.initState();
    _detailFuture = _service.getActivityDetailData(widget.id, (success) {
      ActivityDetailEntity entity = success;
      _data = entity.data.activity;
    }, (fail) {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
          child: AppBar(
            iconTheme: IconThemeData(color: Colors.white),
            automaticallyImplyLeading: true,
            centerTitle: true,
            title: Text(
              "活动详情",
              style: TextStyle(color: Colors.white, fontSize: 16.0),
            ),
          ),
          preferredSize: Size.fromHeight(ScreenUtil.instance.height * 0.036)),
      body: FutureBuilder(
          future: _detailFuture,
          builder: (BuildContext context, AsyncSnapshot asyncSnapshot) {
            switch (asyncSnapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return LoadingDialog();
              default:
                if (asyncSnapshot.hasError)
                  return Container(
                    child: Center(
                      child: Text(
                        Strings.SERVER_EXCEPTION,
                        style: TextStyle(fontSize: 16.0),
                      ),
                    ),
                  );
                else
                  return _detailView();
            }
          }),
//      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }

  Widget _detailView() {
    if (_data == null) {
      return EmptyView();
    }

    return SingleChildScrollView(
      padding: EdgeInsets.only(bottom: 60),
      child: Column(
        children: <Widget>[
          DetailSwiperView(_data.urls, 320),
          Padding(
            padding: EdgeInsets.only(top: 10.0),
          ),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(left: 8),
                  child: Text(
                    _data.name,
                    style: TextStyle(
                        fontSize: 16.0,
                        color: Colors.black54,
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 6.0),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 8, right: 8),
                  child: Html(data: _data.description),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 4.0),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 15.0),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
